//
//  ST_GiftItemView.swift
//  btvideo
//
//  Created by mac on 2020/6/28.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import PINRemoteImage


fileprivate let normal_border_color = UIColor.init(hex: 0xACB4C0)!

fileprivate let selected_border_color = UIColor.init(hex: 0xFEC207)!

class ST_GiftItemView: UIControl {
    
    static let gap = 5.cgFloat
    
    static let columnCount = 4
    
    static let rowCount = 2
    
    static let width = (ScreenWidth - 2*ST_GiftTabCell.landscapeEdge-(ST_GiftItemView.columnCount.cgFloat-1)*ST_GiftItemView.gap)/ST_GiftItemView.columnCount.cgFloat
    
    static let height = (ST_GiftItemView.width-20)+5+41
    
    override var isSelected: Bool{
        didSet{
            if isSelected{
                if CURR_GIFT_ITEM_CELL != self{
                    CURR_GIFT_ITEM_CELL?.refresh_gift_number()
                }
                CURR_GIFT_ITEM_CELL = self
                content_box_view.borderColor = selected_border_color
            }else{
                content_box_view.borderColor = .clear
            }
            
            refresh_gift_number()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(content_box_view)
        content_box_view.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 5, left: 10, bottom: 41, right: 10))
        }
        
        self.addSubview(time_label)
        time_label.snp.makeConstraints { (make) in
            make.left.equalTo(cover_image_view).offset(10)
            make.right.equalTo(cover_image_view).offset(-10)
            make.bottom.equalTo(cover_image_view)
            make.height.equalTo(14)
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.right.centerX.equalToSuperview()
            make.top.equalTo(cover_image_view.snp.bottom).offset(5)
            make.height.equalTo(17)
        }
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.right.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom)
            make.height.equalTo(14)
        }
        
        self.addSubview(gift_type_image_view)
        gift_type_image_view.snp.makeConstraints { (make) in
            make.width.equalTo(24)
            make.height.equalTo(15)
            make.left.top.equalToSuperview()
        }
        
        addSubview(number_box_view)
        number_box_view.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalTo(self.snp.top)
            make.height.equalTo(16)
        }
        number_box_view.addSubview(number_label)
        number_label.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(4)
            make.right.equalToSuperview().offset(-4)
            make.centerY.equalToSuperview()
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    MARK: - Properties
    fileprivate lazy var content_box_view:UIView = {
       let control = UIView()
        control.isUserInteractionEnabled = false
        control.borderColor = .clear
        control.borderWidth = 1
        control.cornerRadius = 10
        
        return control
    }()
    
    fileprivate lazy var number_box_view:UIView = {
       let control = UIView()
        control.isUserInteractionEnabled = false
        control.cornerRadius = 16/2.0
        control.backgroundColor = #colorLiteral(red: 0.9960784314, green: 0.7607843137, blue: 0.02745098039, alpha: 1)
        control.alpha = 0
        return control
    }()
    
    fileprivate lazy var number_label:UILabel = {
        let l = UILabel()
        l.textColor = .white
        l.textAlignment = .center
        l.font = UIFont.systemFont(ofSize: 8, weight: .medium)
        return l
    }()
    
    
      fileprivate lazy var cover_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
            control.clipsToBounds = true
          return control
      }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = .white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#61666F")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var time_label:UILabel = {
        let control = UILabel.init()
        control.backgroundColor = UIColor.init(hex: 0x42CEDA)
        control.textColor = Color.white
        control.textAlignment = .center
        control.cornerRadius = 7
        control.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    

    fileprivate lazy var gift_type_image_view:UIImageView = {
        let control = UIImageView()
        return control
    }()
    
    func refresh_gift_number(){
        
        if child_model?.tabType == .sweetGuard || child_model?.isBlindBox == true{
            number_box_view.alpha = 0
            return
        }
        
        number_box_view.alpha = isSelected ? 1 : 0
        
        number_label.text = "X " + GIFT_NUMBER.string
    }
    
    
    
    var child_model:ST_BTGiftModel?{
        didSet{
            guard let m = child_model else{
                return
            }
            
            if let imageName = m.giftLeftTopWidgetImageName,let img = UIImage(named: imageName){
                gift_type_image_view.image = img
            }else{
                gift_type_image_view.image = nil
            }
            
            
            cover_image_view.pin_setImage(from: m.imageUrl.url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = m.title
            
            content_label.text = m.content
            
            time_label.text = m.durDayStr
            time_label.isHidden = m.durDayStr.count < 1
            
            self.isSelected = m.isSelected
            
        }
    }
}




class ST_GiftMemberItemView: UIControl {
    
    static let horizontalGap = 5.cgFloat
    
    static let verticalGap = 5.cgFloat
    
    static let columnCount = 2
    
    static let rowCount = 2
    
    static let width = (ScreenWidth - ST_GiftMemberItemView.horizontalGap - 10*2)/2.0
    
    static let height = iPhoneXSeries ? 117.cgFloat : 110.cgFloat

    override var isSelected: Bool{
        didSet{
            
            self.borderColor = isSelected ? selected_border_color : normal_border_color
            
            [pur_months_label,pur_months_unit_label,curr_price_label,curr_price_unit_label].forEach({$0.textColor = isSelected ? selectedColor : normalColor })
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.borderColor = normal_border_color
        self.borderWidth = 1
        self.cornerRadius = 10
        
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(36)
            make.bottom.equalToSuperview().offset(-36)
            make.width.equalTo(cover_image_view.snp.height)
        }
        
        self.addSubview(pur_months_label)
        pur_months_label.snp.makeConstraints { (make) in
            make.left.equalTo(cover_image_view.snp.right).offset(10)
            make.height.equalTo(28)
            make.top.equalTo(cover_image_view).offset(-6)
        }
        
        self.addSubview(pur_months_unit_label)
        pur_months_unit_label.snp.makeConstraints { (make) in
            make.left.equalTo(pur_months_label.snp.right)
            make.bottom.equalTo(pur_months_label).offset(-5)
        }
        
        self.addSubview(curr_price_label)
        self.addSubview(curr_price_unit_label)
        curr_price_unit_label.snp.makeConstraints { (make) in
            make.left.equalTo(pur_months_label)
            make.bottom.equalTo(curr_price_label).offset(-4)
        }
        
        curr_price_label.snp.makeConstraints { (make) in
            make.left.equalTo(curr_price_unit_label.snp.right)
            make.top.equalTo(pur_months_label.snp.bottom)
            make.height.equalTo(28)
        }
        
        self.addSubview(current_price_monthly_label)
        
        current_price_monthly_label.snp.makeConstraints { (make) in
            make.left.equalTo(curr_price_label.snp.right).offset(2)
            make.bottom.equalTo(curr_price_label).offset(-4)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    MARK: - Properties
    
    
      fileprivate lazy var cover_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
            control.clipsToBounds = true
          return control
      }()
    
    fileprivate let selectedColor = Color.init(hex: 0xFEC207)
    
    fileprivate let normalColor = Color.white
    
    fileprivate lazy var pur_months_label:UILabel = {
        let control = UILabel.init()
        control.textColor = normalColor
        control.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var pur_months_unit_label:UILabel = {
        let control = UILabel.init()
        control.text = "个月会员"
        control.textColor = normalColor
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var curr_price_label:UILabel = {
        let control = UILabel.init()
        control.textColor = normalColor
        control.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var curr_price_unit_label:UILabel = {
        let control = UILabel.init()
        control.text = "￥"
        control.textColor = normalColor
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var current_price_monthly_label:UILabel = {
        let control = UILabel.init()
        control.textColor  = Color.init(hex: 0xACB4C0)
        control.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.regular)
        return control
    }()
    
    var child_model:ST_BTGiftModel?{
        didSet{
            guard let m = child_model else{
                return
            }
            
            cover_image_view.image = UIImage.init(named: m.imageLocal)
            
            curr_price_label.text = m.currentPrice.string
            
            pur_months_label.text = m.purMonths.string
            
            current_price_monthly_label.text = m.currentPriceMonthlyStr
            
            self.isSelected = m.isSelected
        }
    }
}
